import uvicorn
import os
import sys
import logging

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.StreamHandler(),
        logging.FileHandler('backend.log', encoding='utf-8')
    ]
)
logger = logging.getLogger("Backend-Server")

if __name__ == "__main__":
    try:
        # 确保工作目录正确
        script_dir = os.path.dirname(os.path.abspath(__file__))
        os.chdir(script_dir)
        
        # 创建必要的目录
        os.makedirs(os.path.join("backend", "data", "processes"), exist_ok=True)
        
        logger.info("正在启动后端服务...")
        # 启动FastAPI应用
        uvicorn.run(
            "backend.main:app",
            host="127.0.0.1",
            port=8000,
            reload=False
        )
    
    except Exception as e:
        logger.error(f"启动后端服务时出错: {str(e)}")
        sys.exit(1) 